/**
 * @description uvlog控制器
 * @author 韩
 */

const dayjs = require("dayjs");
const model = require("../../../model/webSite/vistorList/vistorlogs-model")
const { getFilter } = require("../../../utils/setFilter")

// 添加记录
const addUVLOG = async ({
    dateTime,
    pageUrl = null, ip = null, endDateTime = null, startDateTime = null,
}) => {
    try {
        const params = {

            createDateTime: dayjs(),
            ip: "192.168.1.1",
            location: "中国 广东省 广州市",
            dateTime: dateTime,
            sourceChannel: "谷歌搜索",
            //设备
            device: "pc",
            //来源渠道
            sourceChannel: "谷歌搜索",
            //页面标题
            pageTitle: "首页",
            //页面url
            pageUrl: "http://www.baidu.com",
            //创建时间
            createDateTime: dayjs(),
            //更新时间
            updateDateTime: dayjs(),
            //创建人
            createBy: "admin",
            //更新人
            updateBy: "admin",
            //创建人id
            createById: "1",
            //更新人id
            updateById: "1",
            //创建人ip
            createByIp: "192.168.1.1",
            //更新人ip
            updateByIp: "192.168.1.1",
            //创建人位置
            createByLocation: "中国 广东省 广州市",
            //更新人位置
            updateByLocation: "中国 广东省 广州市",
            //创建人浏览器
            createByBrowser: "谷歌浏览器",
            //更新人浏览器
            updateByBrowser: "谷歌浏览器",
            //创建人操作系统
            createByOs: "windows",
            //更新人操作系统
            updateByOs: "windows",
            
        }
        let addRes = null;
        // 添加创建
        addRes = await model.create({
            ...params
        })

        if (addRes) {
            return {
                code: "0000",
                msg: "成功",
            }
        }
        return {
            code: "2000",
            msg: "失败",
        }
    } catch (error) {
        console.log("添加失败", error)
        return {
            code: 100,
            msg: "服务器故障"
        }
    }
}

// 查询列表 
const queryUVLOG = async ({ pageUrl = null, ip = null, endDateTime = null, startDateTime = null, pageSize = 10, current = 1, }) => {
    try {
        let filter = {
            ip: ip && { $eq: ip },
            dateTime: (endDateTime && startDateTime) && { $gte: startDateTime, $lte: endDateTime },
            pageUrl: pageUrl && { $regex: eval("/" + pageUrl + "/i") },
        }
        filter = getFilter(filter)
        const res = await model.find(filter).sort({ createDate: -1 }).skip((current - 1) * pageSize).limit(pageSize).lean()
        const totalCount = await model.find(filter).countDocuments()
        return {
            code: 0,
            msg: "查询成功",
            data: {
                list: res,
                totalCount
            }
        }
    } catch (error) {
        return {
            code: 100,
            msg: "服务器故障"
        }
    }
}


//查询指定时间段内每天的访客数量，并按天分组，相同ip只记为1个访客
const queryUVLOGByDay = async ({ dateType,pageUrl = null, ip = null, endDateTime = null, startDateTime = null, pageSize = 10, current = 1, }) => {
    try {
        let filter = {
            ip: ip && { $eq: ip },
            dateTime: (endDateTime && startDateTime) && { $gte: startDateTime, $lte: endDateTime },
            pageUrl: pageUrl && { $regex: eval("/" + pageUrl + "/i") },
        }
        filter = getFilter(filter)
        const res = await model.aggregate([
            {
                $match: filter
            },
            {
                $group: {
                    _id: "$dateTime",
                    uvCount: { $sum: 1 }
                }
            },
            {
                $sort: { _id: 1 }
            }
        ])
        return {
            code: 0,
            msg: "查询成功",
            data: res
        }
    } catch (error) {
        return {
            code: 100,
            msg: "服务器故障"
        }
    }
}
module.exports = {
    addUVLOG, queryUVLOG,queryUVLOGByDay
}